package com.yusp75.myexam

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout

import com.kc.openset.ad.listener.OSETSplashAdLoadListener
import com.kc.openset.ad.listener.OSETSplashListener
import com.kc.openset.ad.splash.OSETSplash
import com.kc.openset.ad.splash.OSETSplashAd

@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {
    private val TAG = "SplashActivity"
    private lateinit var fl: FrameLayout
    private val handler = Handler(Looper.getMainLooper())
    private val POS_ID_SPLASH = "6F9CC2342C9D9B333808E710E41FF608"
    private var adLoadTimeoutRunnable: Runnable? = null

    private lateinit var placeholder: ConstraintLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_splash)

        placeholder = findViewById(R.id.placeholder)
        fl = findViewById(R.id.fl)

        val startTime = System.currentTimeMillis()
        // 设置超时跳转（5秒）
        adLoadTimeoutRunnable = Runnable {
            navigateToMain()
        }.also {
            handler.postDelayed(it, 5000)
        }

        OSETSplash.getInstance().apply {
            setContext(this@SplashActivity)
            setPosId(POS_ID_SPLASH)
            loadAd(object : OSETSplashAdLoadListener {
                override fun onLoadSuccess(ad: OSETSplashAd) {
                    placeholder.visibility = View.GONE
                    adLoadTimeoutRunnable?.let { handler.removeCallbacks(it) }
                    ad.showAd(this@SplashActivity, fl, createAdListener(startTime))
                }

                override fun onLoadFail(code: String, msg: String) {
                    Log.w(TAG, "Ad load failed: $code - $msg")
                    placeholder.visibility = View.GONE
                    navigateToMain()
                }
            })
        }
    }

    private fun createAdListener(startTime: Long) = object : OSETSplashListener {
        override fun onAdDetailViewClosed() = Unit
        override fun onClick() {
            Log.d(TAG, "Ad clicked")
        }

        override fun onClose() = navigateToMain()
        override fun onShow() {
            Log.d(TAG, "Ad shown in ${System.currentTimeMillis() - startTime}ms")
        }

        override fun onError(code: String, msg: String) {
            Log.e(TAG, "Ad error: $code - $msg")
            navigateToMain()
        }
    }

    private fun navigateToMain() {
        Handler(Looper.getMainLooper()).postDelayed({
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }, 500)

    }

    override fun onDestroy() {
        adLoadTimeoutRunnable?.let { handler.removeCallbacks(it) }
        super.onDestroy()
    }
}
